package cc.ding.servlet;

import cc.ding.entity.Department;
import cc.ding.entity.Employee;
import cc.ding.entity.Position;
import cc.ding.service.EnrollService;
import cc.ding.service.Impl.EnrollServiceImpl;
import cc.ding.util.DateUtil;
import com.alibaba.fastjson.JSON;
import javafx.geometry.Pos;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/staffmanage.action")
public class StaffmanageServlet extends HttpServlet {
    EnrollService enrollService = new EnrollServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String opr = (req.getParameter("opr") == null || "".equals(req.getParameter("opr")))
                ? "list" : req.getParameter("opr");

        switch (opr) {
            case "list1":
                List<Department> departmentList = enrollService.selectDepart();
                String json = JSON.toJSONString(departmentList);
                resp.getWriter().print(json);
                break;
//                return;
            case "list2":
                List<Position> positionList = enrollService.selectPosition();
                String json2 = JSON.toJSONString(positionList);
                resp.getWriter().print(json2);
                break;
//                return;
            case "add":
                String employeeNo = req.getParameter("employeeNo");
                String password = req.getParameter("password");
                String stuName = req.getParameter("stuName");
                String image = req.getParameter("image");
                String sex = req.getParameter("sex");
                String birthday = req.getParameter("birthday");
                String depart = req.getParameter("depart");
                String position = req.getParameter("position");
                String phone = req.getParameter("phone");
                String email = req.getParameter("email");

                Employee employee = new Employee();
                employee.setEmployeeNo(employeeNo);
                employee.setPassword(password);
                employee.setName(stuName);
                employee.setImgPath(image);
                employee.setSex(sex);
                employee.setBirthday(DateUtil.stringDateFormat(birthday, "yyyy-MM-dd"));
                employee.setDepId(Integer.parseInt(depart));
                employee.setPositionId(Integer.parseInt(position));
                employee.setPhone(phone);
                employee.setEmail(email);
                boolean flag = enrollService.add(employee);
                Map map = new HashMap();
                if (flag) {
                    map.put("code", "ok");
                } else {
                    map.put("code", "error");
                }
                resp.getWriter().print(JSON.toJSONString(map));
                break;
        }
    }
}
